home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / mvdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-21  |  4.8 KB  |  172 lines

  1. /* mvdir -- rename directory
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Helper program for GNU mv on machines that lack the rename system call.
  19.  
  20.    Usage: mvdir from to
  21.  
  22.    FROM must be an existing directory.
  23.    TO must not exist, but its parent must exist.
  24.  
  25.    Must be setuid root.
  26.  
  27.    Ian Dall (ian@sibyl.eleceng.ua.oz.au)
  28.    and David MacKenzie (djm@ai.mit.edu) */
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <signal.h>
  33. #include "system.h"
  34.  
  35. #ifndef HIPRI
  36. #define HIPRI -10
  37. #endif
  38.  
  39. #ifdef DEBUG
  40. #define link(FROM, TO) (printf("Linking %s to %s\n", FROM, TO), 0)
  41. #define unlink(FILE) (printf("Unlinking %s\n", FILE), 0)
  42. #endif
  43.  
  44. /* The name this program was run with. */
  45. char *program_name;
  46.  
  47. char *basename ();
  48. char *fullpath ();
  49. char *dirname ();
  50. char *xmalloc ();
  51. void error ();
  52. void strip_trailing_slashes ();
  53.  
  54. void
  55. main (argc, argv)
  56.      int argc;
  57.      char **argv;
  58. {
  59.   char *from, *from_parent, *from_base;
  60.   char *to, *to_parent, *to_parent_path;
  61.   struct stat from_stats, to_stats;
  62.   char *slash, temp;
  63.   int last_elem;
  64.   int i;
  65.  
  66.   program_name = argv[0];
  67.   if (argc != 3)
  68.     {
  69.       fprintf (stderr, "Usage: %s existing-dir new-dir\n", program_name);
  70.       exit (2);
  71.     }
  72.   from = argv[1];
  73.   to = argv[2];
  74.   strip_trailing_slashes (from);
  75.   strip_trailing_slashes (to);
  76.   from_parent = dirname (from);
  77.   to_parent = dirname (to);
  78.   if (!from_parent || !to_parent)
  79.     error (2, 0, "virtual memory exhausted");
  80.  
  81.   /* Make sure `from' is not "." or "..". */
  82.   from_base = basename (from);
  83.   if (!strcmp (from_base, ".") || !strcmp (from_base, ".."))
  84.     error (1, 0, "cannot rename `.' or `..'");
  85.   
  86.   /* Even with an effective uid of root, link fails if the target exists.
  87.      That is what we want, so don't unlink `to' first.
  88.      However, we do need to check that the directories that link and unlink
  89.      will modify exist and are writable by the user. */
  90.  
  91.   if (stat (from, &from_stats))
  92.     error (1, errno, "%s", from);
  93.   if (!S_ISDIR (from_stats.st_mode))
  94.     error (1, 0, "`%s' is not a directory", from);
  95.   if (access (from_parent, W_OK))
  96.     error (1, errno, "cannot write to `%s'", from_parent);
  97.   if (access (to_parent, W_OK))
  98.     error (1, errno, "cannot write to `%s'", to_parent);
  99.  
  100.   /* To prevent disconnecting the tree rooted at `from' from its parent,
  101.      quit if any of the directories in `to' are the same (dev and ino)
  102.      as the directory `from'. */
  103.   
  104.   slash = to_parent_path = fullpath (to_parent);
  105.   last_elem = 0;
  106.   while (1)
  107.     {
  108.       while (*slash == '/')
  109.     ++slash;
  110.       slash = index (slash, '/');
  111.       if (slash != NULL)
  112.     *slash = '\0';
  113.       else
  114.     last_elem = 1;
  115.       if (stat (to_parent_path, &to_stats))
  116.     error (1, errno, "%s", to_parent_path);
  117.       if (to_stats.st_dev == from_stats.st_dev
  118.       && to_stats.st_ino == from_stats.st_ino)
  119.     error (1, 0, "`%s' is an ancestor of `%s'", from, to);
  120.       if (last_elem)
  121.     break;
  122.       *slash++ = '/';
  123.     }
  124.  
  125.   /* We can't make the renaming atomic, but we do our best. */
  126.   for (i = NSIG; i > 0; i--)
  127.     if (i != SIGKILL)
  128.       signal (i, SIG_IGN);
  129.   setuid (0);            /* Make real uid 0 so it is harder to kill. */
  130.   nice (HIPRI - nice (0));    /* Raise priority. */
  131.  
  132.   if (link (from, to))
  133.     error (1, errno, "cannot link `%s' to `%s'", from, to);
  134.   if (unlink (from))
  135.     error (1, errno, "cannot unlink `%s'", from);
  136.  
  137.   /* Replace the directory's `..' entry.  It used to be a link to
  138.      the parent of `from'; make it a link to the parent of `to' instead. */
  139.   i = strlen (to);
  140.   slash = xmalloc (i + 4);
  141.   strcpy (slash, to);
  142.   strcpy (slash + i, "/..");
  143.   if (unlink (slash) && errno != ENOENT)
  144.     error (1, errno, "cannot unlink `%s'", slash);
  145.   if (link (to_parent, slash))
  146.     error (1, errno, "cannot link `%s' to `%s'", to_parent, slash);
  147.  
  148.   exit (0);
  149. }
  150.  
  151. /* Return the full pathname (from /) of the directory DIR,
  152.    as static data. */
  153.  
  154. char *
  155. fullpath (dir)
  156.      char *dir;
  157. {
  158.   char wd[PATH_MAX + 2];
  159.   static char path[PATH_MAX + 2];
  160.  
  161.   if (getwd (wd) == NULL)
  162.     error (1, errno, "cannot get current directory");
  163.   if (chdir (dir))
  164.     error (1, errno, "%s", dir);
  165.   if (getwd (path) == NULL)
  166.     error (1, errno, "cannot get current directory");
  167.   if (chdir (wd))
  168.     error (1, errno, "%s", wd);
  169.  
  170.   return path;
  171. }
  172.